package com.bdk.middle.supplier.web;


import com.bdk.middle.common.domain.PageDO;
import com.bdk.middle.common.result.ResultDO;
import com.bdk.middle.common.result.web.ObjectResponse;
import com.bdk.middle.supplier.api.domain.SupplierDO;
import com.bdk.middle.supplier.api.query.SupplierQueryDO;
import com.bdk.middle.supplier.service.SupplierService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reagan
 * @since 2021-03-19
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @ApiOperation(value="供应单新增", notes="供应单新增")
    @RequestMapping(value="/add.html", method = RequestMethod.POST)
    public @ResponseBody
    ObjectResponse add(@RequestBody SupplierDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = supplierService.add(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="供应单编辑", notes="根据json的id来指定编辑对象")
    @RequestMapping(value="/edit.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse edit(@RequestBody SupplierDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = supplierService.edit(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="供应单删除", notes="根据json的id来指定删除对象")
    @RequestMapping(value="/delete.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse delete(@RequestBody SupplierDO domain) {

        ObjectResponse response = new ObjectResponse();

        ResultDO result = supplierService.remove(domain.getId(), null);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }
    @ApiOperation(value="供应单详情", notes="根据json的id来指定对象")
    @RequestMapping(value="/detail.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse detail(@RequestBody SupplierDO domain) {

        ObjectResponse<SupplierDO> response = new ObjectResponse();

        ResultDO result = supplierService.get(domain.getId());
        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((SupplierDO) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }
    @ApiOperation(value="供应单列表", notes="获取供应单列表")
    @RequestMapping(value="/list.html", method = RequestMethod.POST)
    public @ResponseBody ObjectResponse list(@RequestBody SupplierQueryDO domain) {

        ObjectResponse<PageDO<SupplierDO>> response = new ObjectResponse();

        ResultDO result = supplierService.find(domain);

        if(!result.isSuccess()) {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        } else {
            response.setData((PageDO<SupplierDO>) result.getModel(ResultDO.FIRST_MODEL_KEY));
        }
        return response;
    }

}

